<?php

namespace fastflow\lib;

class auth
{
    public function checkStartAuth($flow_id)
    {
        $lib = new lib();
        $flowAuth_rows = $lib->getAuthRow(['flow_id' => $flow_id]);
        if ($flowAuth_rows) {
            $flowAuth_row = $flowAuth_rows[0];
        } else {
            return false;
        }
        if ($flowAuth_row['rules'] == '') {
            return false;
        }
        $rules = json_decode($flowAuth_row['rules'], true);
        if (!isset($rules['start'])) {
            return false;
        }
        $startRules = $rules['start'];
        if ($startRules == '') {
            return false;
        }

        $user_id = $lib->getSessionUid();
        $groupAaccess_config = config::getConfig('group_access');
        $groupAccess = $lib->getGroupAccess([$groupAaccess_config['uid']=>$user_id]);

        foreach ($startRules as $startRule) {
            if ($startRule['behavior'] == 1) {
                if ($startRule['worker'] == '') {
                    return true;
                }
                $startRule['worker'] = explode(',', $startRule['worker']);
                if ($startRule['scope'] == 1) {
                    if (in_array($user_id, $startRule['worker'])) {
                        return true;
                    }
                } elseif ($startRule['scope'] == 2) {
                    foreach ($groupAccess as $access) {
                        $groupId = $access[$groupAaccess_config['group_id']];
                        if (in_array($groupId, $startRule['worker'])) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    //允许编辑的字段 如果为true允许编辑全表字段
    public function getCanEditFields($bill, $bill_id, $thread_id = null)
    {
        $lib = new lib();
        $can_edit_fields = [];

        $isSuperManager = false;
        $user_id = $lib->getSessionUid();
        $groupAaccess_config = config::getConfig('group_access');
        $groupAccess = $lib->getGroupAccess([$groupAaccess_config['uid']=>$user_id]);
        foreach ($groupAccess as $access) {
            $groupId = $access[$groupAaccess_config['group_id']];
            $group_rows = $lib->getGroup($groupId);
            if ($group_rows)
            {
                $group = $group_rows[0];
            }
            if ($group['pid'] == 0) {
                $isSuperManager = true;
            }
        }
        if ($isSuperManager) {
            return true;
        }

        $bill_row = $lib->getBillRow($bill, $bill_id);
        if (!$bill_row) {
            return [];
        }
        $bill_auth_info = $lib->getBillAuthInfo($bill_row['initiator_id'], $bill, $bill_id);

        $code = $bill_auth_info['code'];

        if ($code == 0) {
            return true;
        } elseif ($code == 1) {
            $thread = $lib->getThreadById($thread_id);
            if (!$thread) {
                return [];
            } else {
                if ($thread['status'] != 1) {
                    return [];
                } else {
                    if ($lib->checkThreadAuth($thread) == false) {
                        return [];
                    }
                }
            }
            $bill_process = $lib->getProcessByBill($bill, $bill_id);
            $flow_id = $bill_process['flow_id'];
            $flowAuth_rows = $lib->getAuthCopyRow(['flow_id' => $flow_id]);
            if (!$flowAuth_rows) {
                return [];
            }
            $flowAuth_row = $flowAuth_rows[0];
            if ($flowAuth_row['rules'] == '') {
                return false;
            }
            $rules = json_decode($flowAuth_row['rules'], true);
            if (!isset($rules['edit'])) {
                return false;
            }
            $editRules = $rules['edit'];
            if ($editRules == '') {
                return [];
            }

            foreach ($editRules as $editRule) {
                if ($editRule['steps'] == '') {
                    if ($editRule['fields'] == '') {
                        return true;
                    } else {
                        $can_edit_fields = array_merge($can_edit_fields, explode(',', $editRule['fields']));
                    }
                } else {
                    if (in_array($thread['step_id'], explode(',', $editRule['steps']))) {
                        if ($editRule['fields'] == '') {
                            return true;
                        } else {
                            $can_edit_fields = array_merge($can_edit_fields, explode(',', $editRule['fields']));
                        }
                    }
                }

            }
            return array_unique($can_edit_fields);
        }

        return [];

    }


    public function getBillFields($bill)
    {
        $lib = new lib();
        $bill_fileds = $lib->getFieldsWithComment($bill);
        $fields = [];
        foreach ($bill_fileds as $bill_filed) {
            $fields[] = $bill_filed['field'];
        }
        return $fields;
    }

}